// pages/user-course/user-course-detail/user-course-detail.js
const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    qiniu: constant.resource.qiniu
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid),
      courseRoundId: options.courseRoundId
    })
    debugger
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '?unionid=' + that.data.unionid,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            courseRound: res.data.data.courseRound,
            experience: res.data.data.experience,
            needTarget: res.data.data.needTarget,
            give: res.data.data.give
          });

          if (res.data.data.schedules) {
            that.setData({
              schedules: res.data.data.schedules
            });
          }

          var showCourse = false;
          if (res.data.data.plannedCourse || res.data.data.plannedCourse == 0) {
            showCourse = true;
            that.setData({
              plannedCourse: res.data.data.plannedCourse,
              restCourse: res.data.data.restCourse
            })
          }

          that.setData({
            showCourse: showCourse
          })
        } else {
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})